package com.star.carservice.client.fragment.base;

import com.star.carservice.client.AppController;
import com.star.carservice.client.fragment.parts.BannerFragment;
import com.star.carservice.client.utils.AsyncTaskUtils;
import com.star.carserviceapi.IClientApi;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragment extends Fragment {

	@SuppressWarnings("unused")
	private final static String TAG = BannerFragment.class.getSimpleName();

	public abstract String getLogTag();

	protected View mCacheView = null;

	protected boolean CacheViewIsNull() {
		
		return mCacheView == null;
	}

	public boolean IsRetainInstance() {
		return true;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(IsRetainInstance());

		Log.i(getLogTag(), "onCreate");
	}

	protected void removeCacheView() {

		ViewGroup parent = (ViewGroup) mCacheView.getParent();

		if (parent != null) {
			parent.removeView(mCacheView);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i(getLogTag(), "onCreateView mCacheView==null:"
				+ (mCacheView == null));

		// if (mCacheView == null) {
		// mCacheView = instanceView(inflater, container, savedInstanceState);
		// }
		//
		// ViewGroup parent = (ViewGroup) mCacheView.getParent();
		//
		// if (parent != null) {
		// parent.removeView(mCacheView);
		// }
		//
		// ButterKnife.inject(this, mCacheView);
		//
		// onInitView(mCacheView);

		return null;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.i(getLogTag(), "onActivityCreated");

		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		Log.i(getLogTag(), "onAttach");
		super.onAttach(activity);
	}

	@Override
	public void onDetach() {
		Log.i(getLogTag(), "onDetach");
		super.onDetach();
	}

	@Override
	public void onDestroy() {
		Log.i(getLogTag(), "onDestroy");
		super.onDestroy();
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		Log.i(getLogTag(), "setUserVisibleHint:" + isVisibleToUser);
	}

	@Override
	public void onPause() {
		Log.i(getLogTag(), "onPause");
		super.onPause();
	}

	@Override
	public void onResume() {
		Log.i(getLogTag(), "onResume");
		super.onResume();
	}

	public <Params, Progress, Result> AsyncTask<Params, Progress, Result> execAsyncTask(
			AsyncTask<Params, Progress, Result> task, Params... params) {

		return AsyncTaskUtils.executeConcurrently(task, params);
	}
	
	protected IClientApi getApi() {
		
		return AppController.getInstance().getApi();
		
	}
	
	protected void  finishActivity() {
		Activity act=getActivity();
		if(act!=null)
			act.finish();
	}
}
